<!--
 * @Description: 首页
 * @Author: kavanaCCW
 * @Date: 2024/7/4
 -->
<template>
  <div class="home">
    这是首页
    <div class="demo">{{ $t('main.hello') }}</div>

    <el-button @click="chooseLanguage">点击切换语言</el-button>

    <div class="iconfont icon-add" style="color:#fff"></div>
    <el-button @click="toGetConfig">读取配置文件</el-button>
    <el-button @click="toSetConfig">修改配置文件</el-button>
    <el-button @click="toSetTop">使窗口保持在最前端</el-button>
    <el-button @click="toOpenNewWindow">新增一个窗口</el-button>
    <el-button @click="toOpenFileDirect">打开文件夹</el-button>
    <el-button @click="toOpenFile">打开文件</el-button>

    <!--<v-md-editor v-model="text" height="400px"></v-md-editor>-->
    <div id="vditor"></div>
  </div>
</template>

<script setup lang="ts">
import {onMounted, ref} from 'vue'
// console.log(i18n.t('main.title'))
import { useI18n } from 'vue-i18n';
const { locale ,t} = useI18n();
const text = ref('')
const chooseLanguage = ()=>{
  locale.value ='en'
  console.log(t('main.demo'))
}


import {getConfig,setConfig,toTop,openNewWindow,openFileDirect,openFile} from "../../ipcRenderer";
const configData = ref();
const toGetConfig = async()=>{
  let res = await getConfig()
  console.log(res,'invoke回调')

  // ipcRenderer.send('getFile')
}

const toSetConfig = ()=>{
  configData.value.version = '1.0.1';
  setConfig(JSON.stringify(configData.value))
}

window.electron.ipcRenderer.on('getFileRes',(event,arg:any)=>{
  console.log(event,'event');
  if(arg.code === 0){
    configData.value = JSON.parse(arg.data);
    console.log("读取成功",configData.value)
  }else{
    console.log(arg);
  }
})
window.electron.ipcRenderer.on('setFileRes',(event,arg:any)=>{
  console.log(event,'event');
  if(arg.code === 0){
    console.log("写入成功")
  }
})

import Vditor from "vditor";
import 'vditor/dist/index.css'
const vditor = ref();

onMounted(()=>{
  vditor.value = new Vditor('vditor',{
    height:'400px',
    width:"400px"
  })
})
let isTop = false;
const toSetTop=async ()=>{
  isTop = !isTop
  let res = await toTop(isTop);
  console.log(res,'toTop')
}

const toOpenNewWindow = async ()=>{
  let res = await openNewWindow();
  console.log(res,'openNewWindow')
}

const toOpenFileDirect =async()=>{
  let res = await openFileDirect();
  console.log(res,'openFileDirect')
}
const toOpenFile =async()=>{
  let res = await openFile();
  console.log(res,'openFile')
  if(res){
    vditor.value.setValue(res);
  }
}

</script>

<style lang="scss" scoped>
.home {
  .demo{
    font-size: 1vw;
    color: #fff;
  }
}
</style>
